package com.geeksaga.dao.ibatis.oracle;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.geeksaga.common.util.DateConvertor;
import com.geeksaga.dao.MemoDao;
import com.geeksaga.dao.ibatis.BaseIBatisDao;
import com.geeksaga.domain.Bbs;
import com.geeksaga.domain.Memo;

/**
 * @author newbie
 * @since 0.1
 */
@Repository
public class MemoDaoImpl extends BaseIBatisDao implements MemoDao {

	/* (non-Javadoc)
	 * @see com.geeksaga.dao.BbsMemoDao#add(com.geeksaga.domain.BbsMemo)
	 */
	public Memo add(Memo memo) throws DataAccessException {
        memo.setRegistDate(DateConvertor.getDateFormat());
        memo.setRegistTime(DateConvertor.getTimeFormat());
        memo.setRegistTimestamp(DateConvertor.getDateTimeFormat());
        memo.setModifyDate(DateConvertor.getDateFormat());
        memo.setModifyTime(DateConvertor.getTimeFormat());
        memo.setModifyTimestamp(DateConvertor.getDateTimeFormat());
		
		return (Memo) getSqlMapClientTemplate().insert("MEMO.add", memo);
	}

	/* (non-Javadoc)
	 * @see com.geeksaga.dao.BbsMemoDao#findList(com.geeksaga.domain.Bbs)
	 */
	@SuppressWarnings("unchecked")
	public List<Memo> findList(Memo memo) throws DataAccessException {
		return (List<Memo>) getSqlMapClientTemplate().queryForList("MEMO.findList", memo);
	}

    /* (non-Javadoc)
     * @see com.geeksaga.dao.MemoDao#updateReply(java.lang.String)
     */
    public int updateReply(String sid) throws DataAccessException {
        Map<String, String> param = new HashMap<String, String>();
        param.put("sidMin", String.valueOf(((long)(Long.valueOf(sid) / 100) * 100)));
        param.put("sid", sid);
        
        return getSqlMapClientTemplate().update("MEMO.updateReply", param);
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.dao.BbsMemoDao#confirmPassword(com.geeksaga.domain.BbsMemo)
     */
    public Memo confirmPassword(Memo bbsMemo) throws DataAccessException {
        return (Memo) getSqlMapClientTemplate().queryForObject("MEMO.confirmPassword", bbsMemo);
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.dao.BbsMemoDao#recentList(java.util.Map)
     */
    @SuppressWarnings("unchecked")
    public List<Memo> recentList(Map<String, Object> parameter) throws DataAccessException {
        return (List<Memo>) getSqlMapClientTemplate().queryForList("MEMO.recentList", parameter);
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.dao.BbsMemoDao#updateContent(com.geeksaga.domain.BbsMemo)
     */
    public int updateContent(Memo memo) throws DataAccessException {
        return getSqlMapClientTemplate().delete("MEMO.updateContent", memo);
    }    
    
    /* (non-Javadoc)
     * @see com.geeksaga.dao.BbsMemoDao#remove(com.geeksaga.domain.BbsMemo)
     */
    public int remove(Memo memo) throws DataAccessException {
        return getSqlMapClientTemplate().delete("MEMO.remove", memo);
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.dao.BbsMemoDao#updateDelYn(com.geeksaga.domain.BbsMemo)
     */
    public int updateDelYn(Memo memo) throws DataAccessException {
        return getSqlMapClientTemplate().delete("MEMO.updateDelYn", memo);
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.dao.BbsMemoDao#removeByBbs(com.geeksaga.domain.Bbs)
     */
    public int removeByBbs(Bbs bbs) throws DataAccessException {
        return getSqlMapClientTemplate().delete("MEMO.removeByBbs", bbs);
    }

    /* (non-Javadoc)
     * @see com.geeksaga.dao.BbsMemoDao#updateDelYnByBbs(com.geeksaga.domain.Bbs)
     */
    public int updateDelYnByBbs(Bbs bbs) throws DataAccessException {
        return getSqlMapClientTemplate().delete("MEMO.updateDelYnByBbs", bbs);
    }   
}